using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif
namespace InputGame
{

    public class InputManagerGame : Microsoft.Xna.Framework.GameComponent
    {

        public struct InputState
        {
#if WINDOWS
                public bool click;
                public int mouseX;
                public int mouseY;
                public bool F1;
                public bool Back;
#elif WINDOWS_PHONE
            public bool click;
            public int mouseX;
            public int mouseY;
#endif
        }


        public static InputState inputState;
#if WINDOWS
        MouseState mcurrent;
        MouseState mprevious;
        Game G;
#elif WINDOWS_PHONE
        GestureSample gs;
        int tempx;
        int tempy;
#endif
        public InputManagerGame(Game game)
            : base(game)
        {
#if WINDOWS
                G = game;
                inputState.click = false;
                inputState.mouseX = 0;
                inputState.mouseY = 0;
                inputState.F1 = false;
                inputState.Back = false;
#elif WINDOWS_PHONE
            TouchPanel.DisplayOrientation = DisplayOrientation.Default;
            TouchPanel.DisplayWidth = 800;
            TouchPanel.DisplayHeight = 480;
            TouchPanel.EnabledGestures = GestureType.Tap | GestureType.FreeDrag | GestureType.DragComplete;
            inputState.mouseX = 400;
            inputState.mouseY = 240;
            inputState.click = false;
#endif
        }

        public override void Update(GameTime gameTime)
        {
#if WINDOWS
            mcurrent = Mouse.GetState();
            inputState.mouseX = mcurrent.X;
            inputState.mouseY = mcurrent.Y;
            if (mcurrent.LeftButton == ButtonState.Released && mprevious.LeftButton == ButtonState.Pressed)
                inputState.click = true;
            else
                inputState.click = false;

            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                G.Exit();
            if (Keyboard.GetState().IsKeyDown(Keys.F1))
                inputState.F1 = true;
            else
                inputState.F1 = false;
            if (Keyboard.GetState().IsKeyDown(Keys.Back))
                inputState.Back = true;
            else
                inputState.Back = false;
            mprevious = mcurrent;
#elif WINDOWS_PHONE
            inputState.click = false;
            if (TouchPanel.IsGestureAvailable)
            {
                gs = TouchPanel.ReadGesture();
                switch (gs.GestureType)
                {
                    case GestureType.FreeDrag:
                        inputState.mouseX = (int)gs.Position.X;
                        inputState.mouseY = (int)gs.Position.Y;
                        tempx = (int)gs.Position.X;
                        tempy = (int)gs.Position.Y;
                        break;
                    case GestureType.DragComplete:
                        inputState.mouseX = tempx;
                        inputState.mouseY = tempy;
                        inputState.click = true;
                        break;
                    case GestureType.Tap:
                        inputState.mouseX = (int)gs.Position.X;
                        inputState.mouseY = (int)gs.Position.Y;
                        inputState.click = true;
                        break;
                }
            }
#endif
            base.Update(gameTime);
        }

        public static InputState GetState()
        {
            return inputState;
        }
    }
}
